package com.tengju.support.domain.model.message;

import com.tengju.support.domain.model.resource.StarLevel;
import com.tengju.support.domain.shared.DomainException;
import com.tengju.support.domain.shared.DomainExceptionCode;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 李大仙
 * @date: 2021/6/7 4:06 下午
 */
public class MessageTaskFactory {

    public static MessageTask create(String title, MessageChannel messageChannel, Integer messageType, Integer messageContentType, String imgUrl, String content, String jumpUrl, Integer orientationTypeVal, List<Long> orientationObjectList, Boolean push, LocalDateTime sendTime, String operator) {
        if (StringUtils.isEmpty(title)) {
            throw DomainException.error(DomainExceptionCode.MESSAGE_TITLE_EMPTY);
        }
        if (messageChannel == null) {
            throw DomainException.error(DomainExceptionCode.MESSAGE_CHANNEL_EMPTY);
        }
        if (messageType == null) {
            throw DomainException.error(DomainExceptionCode.MESSAGE_TYPE_EMPTY);
        }
        if (messageContentType == null) {
            throw DomainException.error(DomainExceptionCode.MESSAGE_CONTENT_TYPE_EMPTY);
        }
        if (orientationTypeVal == null) {
            throw DomainException.error(DomainExceptionCode.MESSAGE_ORIENTATION_TYPE_EMPTY);
        }
        if (push == null) {
            throw DomainException.error(DomainExceptionCode.MESSAGE_PUSH_EMPTY);
        }
        if (sendTime == null) {
            throw DomainException.error(DomainExceptionCode.MESSAGE_SEND_TIME_EMPTY);
        }
        if (LocalDateTime.now().compareTo(sendTime) >= 0){
            throw DomainException.error(DomainExceptionCode.MESSAGE_SEND_TIME_ERROR);
        }
        List<Object> orientationObject = new ArrayList<>();
        if (OrientationType.DESIGNATED_USER == OrientationType.fromVal(orientationTypeVal)) {
            if (CollectionUtils.isEmpty(orientationObjectList)){
                throw DomainException.error(DomainExceptionCode.MESSAGE_ORIENTATION_USER_EMPTY_ERROR);
            }
            orientationObjectList = orientationObjectList.stream()
                    .filter(o -> {
                        if (0L >= o){
                            throw DomainException.error(DomainExceptionCode.MESSAGE_ORIENTATION_ZERO_ERROR);
                        }
                        return true;
                    })
                    .distinct()
                    .collect(Collectors.toList());
            orientationObject = Arrays.asList(orientationObjectList.toArray());
        } else if (OrientationType.DESIGNATED_STAR_LEVEL == OrientationType.fromVal(orientationTypeVal)){
            if (CollectionUtils.isEmpty(orientationObjectList)){
                throw DomainException.error(DomainExceptionCode.MESSAGE_ORIENTATION_LEVEL_EMPTY_ERROR);
            }
            orientationObjectList = orientationObjectList.stream()
                    .filter(o -> {
                        if (StarLevel.parseByCode(o.intValue()) == null){
                            throw DomainException.error(DomainExceptionCode.MESSAGE_ORIENTATION_LEVEL_ERROR);
                        }
                        return true;
                    })
                    .distinct()
                    .collect(Collectors.toList());
            orientationObject = Arrays.asList(orientationObjectList.toArray());
        }
        return new MessageTask(null, title,
                messageChannel,
                MessageType.fromVal(messageType),
                MessageContentType.fromVal(messageContentType),
                imgUrl, content, jumpUrl,
                new MessageUserOrientation(OrientationType.fromVal(orientationTypeVal), orientationObject),
                push,
                SendStatus.NOT_SEND,
                operator,
                operator,
                sendTime,
                null, null);
    }
}
